package com.cloudgateway.config;

import com.alibaba.fastjson.JSON;
import com.cloudgateway.config.modul.GatewayDefaultEnum;
import com.common.global.R;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class GatewayR {
    public static Mono<Void> error(ServerWebExchange exchange, String msg) {
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        HttpHeaders headers = response.getHeaders();
        headers.add("Content-type", "application/json;charset=UTF-8");
        R error = R.error(GatewayDefaultEnum.DEFAULT_ERROR.code(), String.format(GatewayDefaultEnum.DEFAULT_ERROR.message(), msg), null);
        DataBuffer buffer = response
                .bufferFactory()
                .wrap(JSON.toJSONBytes(error));
        return response.writeWith(Mono.just(buffer));
    }
}
